1   /*
2    * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions
6    * are met:
7    *
8    *   - Redistributions of source code must retain the above copyright
9    *     notice, this list of conditions and the following disclaimer.
10   *
11   *   - Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in the
13   *     documentation and/or other materials provided with the distribution.
14   *
15   *   - Neither the name of Oracle nor the names of its
16   *     contributors may be used to endorse or promote products derived
17   *     from this software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  
32  
33  import java.awt.Graphics;
34  import java.awt.Font;
35  import java.applet.Applet;
36  import java.awt.event.MouseEvent;
37  import java.awt.event.MouseListener;
38  
39  
40  /**
41   * An applet that displays jittering text on the screen.
42   *
43   * @author Daniel Wyszynski 04/12/95
44   * @author 05/09/95 kwalrath Changed string; added thread suspension
45   * @author 02/06/98 madbot removed use of suspend and resume and cleaned up
46   */
47  @SuppressWarnings("serial")
48  public class NervousText extends Applet implements Runnable, MouseListener {
49  
50      String banner;              // The text to be displayed
51      char bannerChars[];         // The same text as an array of characters
52      char attributes[];          // Character attributes ('^' for superscript)
53      Thread runner = null;       // The thread that is displaying the text
54      boolean threadSuspended;    // True when thread suspended (via mouse click)
55      static final int REGULAR_WD = 15;
56      static final int REGULAR_HT = 36;
57      static final int SMALL_WD = 12;
58      static final int SMALL_HT = 24;
59      Font regularFont = new Font("Serif", Font.BOLD, REGULAR_HT);
60      Font smallFont = new Font("Serif", Font.BOLD, SMALL_HT);
61  
62      @Override
63      public void init() {
64          banner = getParameter("text");
65          if (banner == null) {
66              banner = "HotJava";
67          }
68  
69          int bannerLength = banner.length();
70          StringBuilder bc = new StringBuilder(bannerLength);
71          StringBuilder attrs = new StringBuilder(bannerLength);
72          int wd = 0;
73          for (int i = 0; i < bannerLength; i++) {
74              char c = banner.charAt(i);
75              char a = 0;
76              if (c == '^') {
77                  i++;
78                  if (i < bannerLength) {
79                      c = banner.charAt(i);
80                      a = '^';
81                      wd += SMALL_WD - REGULAR_WD;
82                  } else {
83                      break;
84                  }
85              }
86              bc.append(c);
87              attrs.append(a);
88              wd += REGULAR_WD;
89          }
90  
91          bannerLength = bc.length();
92          bannerChars = new char[bannerLength];
93          attributes = new char[bannerLength];
94          bc.getChars(0, bannerLength, bannerChars, 0);
95          attrs.getChars(0, bannerLength, attributes, 0);
96  
97          threadSuspended = false;
98          resize(wd + 10, 50);
99          addMouseListener(this);
100     }
101 
102     @Override
103     public void destroy() {
104         removeMouseListener(this);
105     }
106 
107     @Override
108     public void start() {
109         runner = new Thread(this);
110         runner.start();
111     }
112 
113     @Override
114     public synchronized void stop() {
115         runner = null;
116         if (threadSuspended) {
117             threadSuspended = false;
118             notify();
119         }
120     }
121 
122     @Override
123     public void run() {
124         Thread me = Thread.currentThread();
125         while (runner == me) {
126             try {
127                 Thread.sleep(100);
128                 synchronized (this) {
129                     while (threadSuspended) {
130                         wait();
131                     }
132                 }
133             } catch (InterruptedException e) {
134             }
135             repaint();
136         }
137     }
138 
139     @Override
140     public void paint(Graphics g) {
141         int length = bannerChars.length;
142         for (int i = 0, x = 0; i < length; i++) {
143             int wd, ht;
144             if (attributes[i] == '^') {
145                 wd = SMALL_WD;
146                 ht = SMALL_HT;
147                 g.setFont(smallFont);
148             } else {
149                 wd = REGULAR_WD;
150                 ht = REGULAR_HT;
151                 g.setFont(regularFont);
152             }
153             int px = (int) (10 * Math.random() + x);
154             int py = (int) (10 * Math.random() + ht);
155             g.drawChars(bannerChars, i, 1, px, py);
156             x += wd;
157         }
158     }
159 
160     @Override
161     public synchronized void mousePressed(MouseEvent e) {
162         e.consume();
163         threadSuspended = !threadSuspended;
164         if (!threadSuspended) {
165             notify();
166         }
167     }
168 
169     @Override
170     public void mouseReleased(MouseEvent e) {
171     }
172 
173     @Override
174     public void mouseEntered(MouseEvent e) {
175     }
176 
177     @Override
178     public void mouseExited(MouseEvent e) {
179     }
180 
181     @Override
182     public void mouseClicked(MouseEvent e) {
183     }
184 
185     @Override
186     public String getAppletInfo() {
187         return "Title: NervousText\nAuthor: Daniel Wyszynski\n"
188                 + "Displays a text banner that jitters.";
189     }
190 
191     @Override
192     public String[][] getParameterInfo() {
193         String pinfo[][] = {
194             { "text", "string", "Text to display" }, };
195         return pinfo;
196     }
197 }